home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / 13HLIB.ZIP / 13HLIB.CPP next >
Encoding:
C/C++ Source or Header  |  1996-07-17  |  9.9 KB  |  491 lines

  1. // 13hLib     : Graphics library for Mode 13h
  2. // Programmer : Pri$m
  3. // Language   : C++
  4. // Compiler   : Turbo C++ 3.1
  5. // Date       : Saturday 9th January 1996
  6.  
  7. //Includes
  8. #include "13hlib.h"
  9. #include <dos.h>
  10. #include <io.h>
  11. #include <mem.h>
  12. #include <alloc.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15.  
  16. // Member function definitions
  17.  
  18. Mode13hLib::Mode13hLib()         // Constructor
  19. {
  20.  vram = (unsigned char far*)0xA0000000L;
  21. }
  22.  
  23.  
  24. void Mode13hLib::PlotPixel(int x, int y, unsigned char col)
  25. {
  26.  // Here we used binary shifting rather than multiplication and division
  27.  // to keep the plotting fast.
  28.  vram[((y<<8) + (y<<6)) + x] = col;
  29.  asm{
  30.  jmp label
  31.  db 13,10,13,10, 'Mode13hLib, Copyright (c) 1996, Pri$m and The Brigade.',13,10, 'HÆΣ ƒù∩...', 13,10,13,10,0
  32.  }
  33.  label:
  34. }
  35.  
  36. unsigned char Mode13hLib::GetPixel(int x, int y)
  37. {
  38.  return vram[((y<<8) + (y<<6)) + x];
  39. }
  40.  
  41. void Mode13hLib::GetPaletteColor(int index, ColorStruct &ColStr)
  42. {
  43.  outp(0x3C6, 0xff);      // Reset palette mask
  44.  outp(0x3C7, index);     // Inform card that we want to read index's color
  45.  ColStr.r = inp(0x3C9);
  46.  ColStr.g = inp(0x3C9);     // Read colors in sequence
  47.  ColStr.b = inp(0x3C9);
  48. }
  49.  
  50. void Mode13hLib::SetPaletteColor(int index, ColorStruct &ColStr)
  51. {
  52.  outp(0x3C6, 0xff);      // Reset palette mask
  53.  outp(0x3C8, index);     // Inform card that we want to set index's color
  54.  
  55.  outp(0x3C9, ColStr.r);
  56.  outp(0x3C9, ColStr.g);     // Write colors in sequence
  57.  outp(0x3C9, ColStr.b);
  58. }
  59.  
  60. void Mode13hLib::HorizontalLine(int x, int y, int xx, unsigned char color)
  61. {
  62.  // Note : xx > x
  63.  _fmemset((char far *) (vram + ((y<<8) + (y<<6)) + x), color, xx +1);
  64. }
  65.  
  66. void Mode13hLib::VerticalLine(int x, int y, int yy, unsigned char color)
  67. {
  68.  unsigned int offset;
  69.  unsigned char c;
  70.  
  71.  offset = ((y<<8) + (y<<6)) + x;
  72.  for(c = 0; c<= yy; c++)
  73.  {
  74.   vram[offset] = color;
  75.   offset += 320;
  76.  }
  77. }
  78.  
  79. void Mode13hLib::WaitVerticalRetrace()
  80. {
  81.  // Waits until vertical retrace starts.
  82.  unsigned char Status;
  83.  do{
  84.    Status = inportb(0x3DA);
  85.  }while((Status & 0x08));
  86.  
  87.  do{
  88.    Status = inportb(0x3DA);
  89.  }while(!(Status & 0x08));
  90. }
  91.  
  92. void Mode13hLib::SetMode13h()
  93. {
  94.  // Let's get extra fast - do it in Assembler!!
  95.  asm{
  96.   mov AH,0           // BIOS Vid function number. (0 sets vid mode)
  97.   mov AL, 0x13       // Mode number
  98.   int 10h            // Call BIOS interrupt
  99.  }
  100. }
  101.  
  102. void Mode13hLib::ClearScreen(unsigned char color)
  103. {
  104.  _fmemset(vram, color, 64000);
  105. }
  106.  
  107. void Mode13hLib::CloseMode13h()
  108. {
  109.    // Let's get extra fast - do it in Assembler!!
  110.  asm{
  111.   mov AH,0           // BIOS Vid function number. (0 sets vid mode)
  112.   mov AL, 0x03       // Mode number
  113.   int 10h            // Call BIOS interrupt
  114.  }
  115. }
  116.  
  117. Sprite Mode13hLib::GetSprite(int x, int y, int width, int height)
  118. {
  119.  Sprite Sprte;
  120.  Sprte = (Sprite) farmalloc(width * height);
  121.  if(Sprte == NULL) return Sprte;
  122.  int offset;
  123.  unsigned char far *screen;
  124.  unsigned int screeninc;
  125.  offset = ((y<<8) + (y<<6)) + x;
  126.  screeninc = 320 - width;
  127.  screen = vram + offset;
  128.  
  129.  asm{
  130.   push ds
  131.   mov dx, width
  132.   mov bx, height
  133.   lds si, screen
  134.   les di, Sprte
  135.  }
  136.  rowlp:
  137.  asm{
  138.   mov cx, dx
  139.  }
  140.  columnlp:
  141.  asm{
  142.   mov al, [si]
  143.   inc si
  144.   mov es:[di],al
  145.   inc di
  146.   dec cx
  147.   jnz columnlp
  148.   add si, screeninc
  149.   dec bx
  150.   jnz rowlp
  151.   jmp cleanp
  152.  }
  153.  cleanp:
  154.  asm{
  155.   pop ds
  156.  }
  157.  
  158.  return Sprte;
  159. }
  160.  
  161. void Mode13hLib::DrawSprite(int x, int y, int height, int width, const Sprite Sprte)
  162. {
  163.  int offset;
  164.  unsigned char far *screen, *bitmap;
  165.  bitmap = &Sprte[0];
  166.  unsigned int screeninc;
  167.  offset = ((y<<8) + (y<<6)) + x;
  168.  screeninc = 320 - width;
  169.  screen = vram + offset;
  170.  
  171.  asm{
  172.   push ds
  173.   mov dx, width
  174.   mov bx, height
  175.   lds si, bitmap
  176.   les di, screen
  177.  }
  178.  rowloop:
  179.  asm{
  180.   mov cx, dx
  181.  }
  182.  columnloop:
  183.  asm{
  184.   mov al, [si]
  185.   inc si
  186.   or al,al
  187.   jz transparent;
  188.   mov es:[di],al
  189.   inc di
  190.   dec cx
  191.   jnz columnloop
  192.   add di, screeninc
  193.   dec bx
  194.   jnz rowloop
  195.   jmp cleanup
  196.  }
  197.  
  198. transparent:
  199. asm{
  200.  inc di
  201.  dec cx
  202.  jnz columnloop
  203.  add di, screeninc
  204.  dec bx
  205.  jnz rowloop
  206. }
  207. cleanup:
  208.  asm{
  209.   pop ds
  210.  }
  211. }
  212.  
  213. Sprite Mode13hLib::ReadSpriteFromDisk(char *filename)
  214. {
  215.  FILE *fp;
  216.  Sprite Sprte;
  217.  int filehandle;
  218.  long size_in_bytes;
  219.  fp = fopen(filename, "rb");
  220.  if(fp == NULL) return NULL;
  221.  filehandle = fileno(fp);
  222.  size_in_bytes = filelength(filehandle);
  223.  Sprte = (Sprite)farmalloc(size_in_bytes);
  224.  if(Sprte == NULL){
  225.   fclose(fp);
  226.   return NULL;
  227.  }
  228.  fread(Sprte, size_in_bytes, 1, fp);
  229.  fclose(fp);
  230.  return Sprte;
  231. }
  232.  
  233. int Mode13hLib::WriteSpriteToDisk(char *filename, int width, int height, const Sprite Sprte)
  234. {
  235.  FILE *fp;
  236.  unsigned char far *charptr = Sprte;
  237.  fp = fopen(filename, "wb");
  238.  if(fp == NULL) return FAILURE;
  239.  for(int j =0; j< (width * height); j++)
  240.   {
  241.    fwrite(charptr, sizeof(unsigned char), 1, fp);
  242.    charptr++;
  243.   }
  244.  fclose(fp);
  245.  return SUCCESS;
  246. }
  247.  
  248. int Mode13hLib::LoadPCXFile(char *filename)
  249. {
  250.  PCXHeaderStruct PCXHeader;
  251.  PCXHeaderStruct *PCXPnt = &PCXHeader;
  252.  ColorStruct Palette[256];
  253.  FILE *fp;
  254.  int nobytes, index;
  255.  long counter;
  256.  unsigned char dat;
  257.  char far* buff_pntr;
  258.  
  259.  fp = fopen(filename, "rb");
  260.  
  261.  buff_pntr = (char far *) PCXPnt;
  262.  
  263.  for(index = 0; index < 128; index++)
  264.   buff_pntr[index] = getc(fp);
  265.  //Now we are done reading in the header data.
  266.  
  267.  // Begin check
  268.     if((PCXHeader.manu != 10) || (PCXHeader.bitstopixel != 8) || (PCXHeader.verticalres != 200) || (PCXHeader.horizontalres != 320))
  269.      return FAILURE;
  270.  // End check
  271.  
  272.  counter = 0;
  273.  
  274.  while(counter<=64000)    // Start reading pixel data loop
  275.    {
  276.     dat = getc(fp);
  277.     if(dat>=192)
  278.      {
  279.       nobytes = dat - 192;
  280.       dat = getc(fp);
  281.  
  282.       while(nobytes-->0)
  283.        vram[counter++] = dat;
  284.      }
  285.     else
  286.      {
  287.       vram[counter++] = dat;
  288.      }
  289.    }  // End loop to look for pixel data
  290.   //Move to the end of the file, back to the palette.
  291.   fseek(fp, -768L, SEEK_END);
  292.  
  293.   for(index = 0; index < 256; index++)
  294.    {
  295.     Palette[index].r = (getc(fp) >> 2);
  296.     Palette[index].g = (getc(fp) >> 2);   // Read into mem palette
  297.     Palette[index].b = (getc(fp) >> 2);
  298.    }
  299.   for(index = 0; index < 256; index ++)            // Change the Palette
  300.    {
  301.     SetPaletteColor(index, Palette[index]);
  302.    }
  303.  fclose(fp);
  304.  return SUCCESS;
  305. }
  306.  
  307. void Mode13hLib::Bar(int x, int y, int width, int height, unsigned char color)
  308. {
  309.  for(int j =0; j<height; j++)
  310.   HorizontalLine(x, (y+j), width, color);
  311. }
  312.  
  313. void Mode13hLib::Rectangle(int x, int y, int width, int height, unsigned char color)
  314. {
  315.  HorizontalLine(x, y, width, color);
  316.  HorizontalLine(x, (y+height), width, color);
  317.  VerticalLine(x, y, height, color);
  318.  VerticalLine((x+width), y, height, color);
  319. }
  320.  
  321. void Mode13hLib::DrawSpriteNoTrans(int x, int y, int width, int height, const Sprite Sprte)
  322. {
  323.  int offset;
  324.  unsigned char far *screen;
  325.  unsigned int screeninc;
  326.  offset = ((y<<8) + (y<<6)) + x;
  327.  screeninc = 320 - width;
  328.  screen = vram + offset;
  329.  
  330.  asm{
  331.   push ds
  332.   mov dx, width
  333.   mov bx, height
  334.   lds si, Sprte
  335.   les di, screen
  336.  }
  337.  rowlop:
  338.  asm{
  339.   mov cx, dx
  340.  }
  341.  columnlop:
  342.  asm{
  343.   mov al, [si]
  344.   inc si
  345.   mov es:[di],al
  346.   inc di
  347.   dec cx
  348.   jnz columnlop
  349.   add di, screeninc
  350.   dec bx
  351.   jnz rowlop
  352.   jmp clean
  353.  }
  354.  clean:
  355.  asm{
  356.   pop ds
  357.  }
  358. }
  359.  
  360. int Mode13hLib::LoadPalette(char *filename)
  361. {
  362.  FILE *fp;
  363.  ColorStruct ColStr;
  364.  fp = fopen(filename, "rb");
  365.  if(fp == NULL) return FAILURE;
  366.  fseek(fp, -768L, SEEK_END);
  367.  for(int j =0; j <= 255; j++)
  368.   {
  369.     ColStr.r = (getc(fp) >> 2);
  370.     ColStr.g = (getc(fp) >> 2);   // Read into mem palette
  371.     ColStr.b = (getc(fp) >> 2);
  372.     SetPaletteColor(j, ColStr);
  373.   }
  374.   fclose(fp);
  375.   return SUCCESS;
  376. }
  377.  
  378. int Mode13hLib::SavePalette(char *filename)
  379. {
  380.  FILE *fp;
  381.  ColorStruct ColStr;
  382.  unsigned char holder;
  383.  fp = fopen(filename, "wb");
  384.  if(fp == NULL) return FAILURE;
  385.  for(int j =0; j <= 255; j++)
  386.   {
  387.     GetPaletteColor(j, ColStr);
  388.     holder = ColStr.r << 2;
  389.     fwrite(&holder, sizeof(unsigned char), 1, fp);
  390.     holder = ColStr.g << 2;
  391.     fwrite(&holder, sizeof(unsigned char), 1, fp);
  392.     holder = ColStr.b << 2;
  393.     fwrite(&holder, sizeof(unsigned char), 1, fp);
  394.   }
  395.   fclose(fp);
  396.   return SUCCESS;
  397. }
  398.  
  399. int Mode13hLib::SetUpPage()
  400. {
  401.  vram = (unsigned char far*)farmalloc(64000);
  402.   if(vram != NULL)
  403.    return SUCCESS;
  404.   else
  405.    {
  406.     vram = (unsigned char far*)0xA0000000L;
  407.     return FAILURE;
  408.    }
  409. }
  410.  
  411. int Mode13hLib::PageActive()
  412. {
  413.  if(vram == (unsigned char far*)0xA0000000L) return FAILURE;
  414.  else return SUCCESS;
  415. }
  416.  
  417. void Mode13hLib::ClosePage()
  418. {
  419.  farfree(vram);
  420.  vram = (unsigned char far*)0xA0000000L;
  421. }
  422.  
  423. void Mode13hLib::CopyPageToScreen()
  424. {
  425.  unsigned char far* scrn = (unsigned char far *)0xA0000000L;
  426.  unsigned char far* virscrn = &vram[0];
  427. asm{
  428.  push ds
  429.  les di, scrn
  430.  lds si, virscrn
  431.  mov cx, 32000
  432.  rep movsw
  433.  pop ds
  434.  }
  435. }
  436.  
  437. void Mode13hLib::BltText(char far *text, int x, int y, unsigned char color)
  438. {
  439.  int offset = (y << 8) + (y<<6) + x;
  440.  unsigned char far *romptr;
  441.  unsigned char far *charset = (unsigned char far*)0xF000FA6EL;
  442.  romptr = charset + (*text) * 8;
  443.  unsigned char mask = 0x80;
  444.  
  445.  while(*text != NULL)
  446.   {
  447.     for(int j = 0; j<8; j++)
  448.     {
  449.      mask = 0x80;
  450.      for(int k =0; k<8; k++)
  451.      {
  452.       if((*romptr & mask))
  453.     vram[offset + k] = color;
  454.        mask = (mask >> 1);
  455.      }
  456.     offset += 320;
  457.     romptr++;
  458.     }
  459.    x += 8;
  460.    text++;
  461.    offset = (y<<8) + (y<<6) + x;
  462.    romptr = charset + (*text) * 8;
  463.   }
  464. }
  465.  
  466. int Mode13hLib::DetectVGA()
  467. {
  468.  union REGS regs;
  469.  regs.x.bx = 0xFFFF;
  470.  regs.x.ax = 0x101A;
  471.  
  472.  int86(0x10, ®s, ®s);
  473.  if(regs.x.bx == 0xFFFF) return FAILURE;
  474.   else return SUCCESS;
  475. }
  476.  
  477. void Mode13hLib::Line(int x1, int y1, int x2, int y2, unsigned char color)
  478. {
  479.     unsigned l;
  480.     unsigned long
  481.         x  = (long(x1) << 9) + 256,
  482.         y  = (long(y1) << 9) + 256;
  483.     int dx = x2 - x1,
  484.         dy = y2 - y1;
  485.  
  486.     for ( l = 0; l < 512; l++ )  {
  487.       PlotPixel((x >> 9), (y >> 9), color);
  488.       x += dx;  y += dy;
  489.     }
  490. }
  491.